package com.zaicheng.cloud.netty.protocol;
/**
 @author zaicheng
 @version 1.0
 @date 2022-04-19
 @email zzc15847256473@163.com
 */

import com.dyuproject.protostuff.LinkedBuffer;
import com.zaicheng.cloud.netty.RpcRequest;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.channel.ChannelHandler.Sharable;

/**
 * @author 张在成
 */
@Sharable
public class ProtoStuffEncoder extends MessageToByteEncoder<Object> {

    public ProtoStuffEncoder() {
    }

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, Object object, ByteBuf byteBuf) throws Exception {
        LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE);
        try {
            byte[] bytes = ProtoStuffUtil.serializer(object);
            byteBuf.writeBytes(bytes);
        }finally {
            buffer.clear();
        }
    }


}
